<?php
class Muser extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->tablename = 'yun_user';
	}
	
	function get_one($uid) {
		$item = new stdClass();
		$query = $this->db->query('select * from '.$this->tablename.' where uid = '.$uid);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_all($order='uid') {
		$item = new stdClass();
		$query = $this->db->query('select * from '.$this->tablename.' order by '.$order.' limit 36 ');
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	function pagination($uid = 0, $pindex = 0, &$totalcount = 0, $perpage = 10) {
		$item = new stdClass();
		$startnumber = $pindex * $perpage;
		$this->db->limit($perpage, $startnumber);
		$query = $this->db->query('select * from `yun_records` where uk = '.$uid.' order by created desc limit '.$startnumber.','.$perpage);
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}

		$query1 = $this->db->query('select rid from `yun_records` where uk = '.$uid);
		$totalcount = $query1->num_rows() ;
		$query->free_result();
		$query1->free_result();
		return $item;
	}

	function get_list($uid) {
		$item = new stdClass();
		$query = $this->db->query('select * from `yun_records` where uk = '.$uid.' order by created desc limit 20');
		if ($query->num_rows() > 0) {
			$item = $query->result() ;
		}
		$query->free_result();
		return $item;
	}

	// 用户页 左边最热分享资源
	function get_hot_list($uid) {
		$item = new stdClass();
		$query = $this->db->query('select rid,title from `yun_records` where uk = '.$uid.' order by view_count desc limit 10');
		if ($query->num_rows() > 0) {
			$item = $query->result() ;
		}
		$query->free_result();
		return $item;
	}

    function exist_id($uk){
		$item = 0 ;
		$query = $this->db->query('select uid from '.$this->tablename.' where uid = '.$uk);
		if ($query->num_rows() > 0) {
			$item = 1;
		}
		return $item;
	}

    function insert($arr){
		if(!$this->exist_id($arr['uid'])){                                    // 防止重复记录
			$this->db->insert($this->tablename,$arr);
			return  $this->db->insert_id(); 
		}else{
			log_message('error', '-----------------用户--'.$arr['username'].'['.$arr['uid'].']'.' 已存在库里-------------------------------');
			return false;
		}
    }

    function update($id,$arr)
    {
        $this->db->where('uid',$id);
        $this->db->update($this->tablename,$arr);
    }

    function delete($id){
        $this->db->where('uid',$id);
        $this->db->delete($this->tablename);
    }

}